package com.jic.member.impl;

import java.lang.Long;
import java.util.ArrayList;
import java.util.List;

import com.jic.common.base.vo.RestResult;
import com.jic.member.entity.MememberGrowthAppraisal;
import com.jic.member.entity.MememberGrowthOrderMoney;
import com.jic.member.entity.MememberGrowthOrderRecharge;
import com.jic.member.mapper.MememberGrowthAppraisalMapper;
import com.jic.member.mapper.MememberGrowthOrderMoneyMapper;
import com.jic.member.mapper.MememberGrowthOrderRechargeMapper;
import com.jic.member.request.MemberGrowthOrderMoneyRequest;
import com.jic.member.request.MemberGrowthRuleRequest;
import com.jic.member.response.MemberGrowthOrderResponse;
import com.jic.member.response.MemberGrowthRuleResponse;
import com.jic.member.service.MememberGrowthOrderRechargeService;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.Page;


import com.jic.member.service.MememberGrowthRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 会员等级管理-成长值规则-下单-充值赠送表
 * @author : 
 * @email : 
 * @since : 2020-04-10 06:31:28
 * @version : v1.0.0
 */
@Service
public class MememberGrowthOrderRechargeServiceImpl implements MememberGrowthOrderRechargeService{

    @Resource
    private MememberGrowthOrderRechargeMapper mememberGrowthOrderRechargeMapper;
    @Resource
    private MememberGrowthOrderMoneyMapper mememberGrowthOrderMoneyMapper;
    @Resource
    private MememberGrowthAppraisalMapper mememberGrowthAppraisalMapper;
    @Autowired
    private MememberGrowthRuleService mememberGrowthRuleService;


    @Override
    public long insert(MememberGrowthOrderRecharge mememberGrowthOrderRecharge) {
        long flag = mememberGrowthOrderRechargeMapper.insert(mememberGrowthOrderRecharge);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return mememberGrowthOrderRechargeMapper.delete(id);
    }

    @Override
    public long update(MememberGrowthOrderRecharge mememberGrowthOrderRecharge) {
        return mememberGrowthOrderRechargeMapper.update(mememberGrowthOrderRecharge);
    }

    @Override
    public MemberGrowthOrderResponse queryByPrimaryKey(Long id){
        return mememberGrowthOrderRechargeMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MememberGrowthOrderRecharge> query(MememberGrowthOrderRecharge mememberGrowthOrderRecharge){
        return mememberGrowthOrderRechargeMapper.query(mememberGrowthOrderRecharge);
    }

    @Override
    public PageResult<MememberGrowthOrderRecharge> queryPage(MememberGrowthOrderRecharge mememberGrowthOrderRecharge, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<MememberGrowthOrderRecharge> list = mememberGrowthOrderRechargeMapper.queryPage(startIndex, pageQuery.getLimit(), mememberGrowthOrderRecharge);
        long count = mememberGrowthOrderRechargeMapper.queryPageCount(mememberGrowthOrderRecharge);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult<MemberGrowthOrderResponse> getRrowthOrderRuleById(MemberGrowthRuleRequest request) {
        RestResult<MemberGrowthRuleResponse> response =mememberGrowthRuleService.getDeptInfoById(request.getId());
        RestResult  restResult = new RestResult();
        MemberGrowthOrderResponse memberGrowthOrderResponse = new MemberGrowthOrderResponse();
        if(request.getObtainMode().equals("1")){ //如果是下单赠送
             memberGrowthOrderResponse = mememberGrowthOrderRechargeMapper.queryByPrimaryKey(request.getId());
            Long id =memberGrowthOrderResponse.getId();
            MememberGrowthOrderMoney mememberGrowthOrderMoney = new MememberGrowthOrderMoney();
            if(memberGrowthOrderResponse.getConsumeType() == 3){ //如果是每消费
                mememberGrowthOrderMoney = mememberGrowthOrderMoneyMapper.queryByPrimaryKey(id);
                mememberGrowthOrderMoney.setDeptType(Long.valueOf(response.getData().getDeptType()));
                mememberGrowthOrderMoney.setDeptName(response.getData().getDeptName());
                restResult.setData(mememberGrowthOrderMoney);
                restResult.setCode("0000");
            }else { //如果是阶梯消费
                 mememberGrowthOrderMoney.setOrderId(id);
                List<MememberGrowthOrderMoney> orderMoneyList = mememberGrowthOrderMoneyMapper.query(mememberGrowthOrderMoney);
                List<MemberGrowthOrderMoneyRequest> orderHandleSel = new ArrayList<>();
                for(int i = 0; i <orderMoneyList.size();i++){
                    MemberGrowthOrderMoneyRequest mememberGrowthOrderMoney1 = new MemberGrowthOrderMoneyRequest();
                   mememberGrowthOrderMoney1.setConsumeType(6);
                   mememberGrowthOrderMoney1.setConsumeMoney(orderMoneyList.get(i).getConsumeMoney());
                   mememberGrowthOrderMoney1.setGiveGrowthVal(orderMoneyList.get(i).getGiveGrowthVal());
                    orderHandleSel.add(mememberGrowthOrderMoney1);
                }
                memberGrowthOrderResponse.setOrderHandleSel(orderHandleSel);
                memberGrowthOrderResponse.setDeptType(Long.valueOf(response.getData().getDeptType()));
                memberGrowthOrderResponse.setDeptName(response.getData().getDeptName());
                restResult.setData(memberGrowthOrderResponse);
                restResult.setCode("0000");
            }

        } else if(request.getObtainMode().equals("4")){ //如果是充值赠送
            memberGrowthOrderResponse = mememberGrowthOrderRechargeMapper.queryByPrimaryKey(request.getId());
            Long id =memberGrowthOrderResponse.getId();
            MememberGrowthOrderMoney mememberGrowthOrderMoney = new MememberGrowthOrderMoney();
            if(memberGrowthOrderResponse.getCostType() == 3){ //如果是每充值
                mememberGrowthOrderMoney = mememberGrowthOrderMoneyMapper.queryByPrimaryKey(id);
                mememberGrowthOrderMoney.setDeptType(Long.valueOf(response.getData().getDeptType()));
                mememberGrowthOrderMoney.setDeptName(response.getData().getDeptName());
                restResult.setData(mememberGrowthOrderMoney);
                restResult.setCode("0000");
            }else { //如果是阶梯充值
                mememberGrowthOrderMoney.setOrderId(id);
                List<MememberGrowthOrderMoney> orderMoneyList = mememberGrowthOrderMoneyMapper.query(mememberGrowthOrderMoney);
                List<MemberGrowthOrderMoneyRequest> costHandleSel = new ArrayList<>();
                for(int i = 0; i <orderMoneyList.size();i++){
                    MemberGrowthOrderMoneyRequest mememberGrowthOrderMoney1 = new MemberGrowthOrderMoneyRequest();
                    mememberGrowthOrderMoney1.setCostType(6);
                    mememberGrowthOrderMoney1.setConsumeMoney(orderMoneyList.get(i).getConsumeMoney());
                    mememberGrowthOrderMoney1.setGiveGrowthVal(orderMoneyList.get(i).getGiveGrowthVal());
                    costHandleSel.add(mememberGrowthOrderMoney1);
                }
                memberGrowthOrderResponse.setCostHandleSel(costHandleSel);
                memberGrowthOrderResponse.setDeptType(Long.valueOf(response.getData().getDeptType()));
                memberGrowthOrderResponse.setDeptName(response.getData().getDeptName());
                restResult.setData(memberGrowthOrderResponse);
                restResult.setCode("0000");
            }
        } else if(request.getObtainMode().equals("3")){ //如果是评价赠送
            if(request.getWhetherPic() != 3){ //如果是单一选择带图或不带图
                   MememberGrowthAppraisal mememberGrowthAppraisal = mememberGrowthAppraisalMapper.queryByPrimaryKey(request.getId());
                   mememberGrowthAppraisal.setDeptType(response.getData().getDeptType());
                   mememberGrowthAppraisal.setDeptName(response.getData().getDeptName());
                   restResult.setCode("0000");
                   restResult.setData(mememberGrowthAppraisal);
            }else{
                   MememberGrowthAppraisal mememberGrowthAppraisal = new MememberGrowthAppraisal();
                   mememberGrowthAppraisal.setGrowthRuleId(request.getId());
                   List<MememberGrowthAppraisal> mememberGrowthAppraisalList =mememberGrowthAppraisalMapper.query(mememberGrowthAppraisal);
                   for(int i =0;i<mememberGrowthAppraisalList.size();i++){
                       memberGrowthOrderResponse.setAppGiveGrowthVal(mememberGrowthAppraisalList.get(1).getGiveGrowthVal());
                       memberGrowthOrderResponse.setAppGiveGrowthMaxNum(mememberGrowthAppraisalList.get(1).getGiveGrowthMaxNum());
                       memberGrowthOrderResponse.setAppGiveGrowthVals(mememberGrowthAppraisalList.get(0).getGiveGrowthVal());
                       memberGrowthOrderResponse.setAppGiveGrowthMaxNums(mememberGrowthAppraisalList.get(0).getGiveGrowthMaxNum());
                   }
                restResult.setCode("0000");
                   memberGrowthOrderResponse.setWhetherPic(3);
                memberGrowthOrderResponse.setDeptType(Long.valueOf(response.getData().getDeptType()));
                memberGrowthOrderResponse.setDeptName(response.getData().getDeptName());
                restResult.setData(memberGrowthOrderResponse);
            }
        }
        return restResult;
    }

    @Override
    public MemberGrowthOrderResponse queryOrderId(Long id) {
        return mememberGrowthOrderRechargeMapper.queryOrderId(id);
    }
}